<?php
/**
 * Date: 2021/8/4
 * Time: 15:19
 * 日志上报控制器
 */

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Validator;
use App\Service\Login;
use Symfony\Component\HttpFoundation\Response;
use App\Model\LogModel;
use App\Amqp\Log;
use ClickHouseDB;


class ReportLog extends Controller
{

    /*
    * 日志上报
    * $log_id 批次id
    * $event_name 事件说明
    * $datas array
    * $add_time 上报时间
    */
    public function report(Request $request)
    {

         $data=$request->post();


//        Validator::make($data, [
//            'event_name' => 'required',
//            'datas' => 'required',
//            'log_id'=>'log_id'
//        ]);




        $time=time();
        LogModel::insert(['log_id'=>$data['log_id'],'event_name'=>$data['event_name'],'datas'=>json_encode($data['datas']),'add_time'=>$time]);

        return json_encode(['code' => Response::HTTP_OK, 'msg' => '成功']);


    }


    public function uploadFile(Request $request){

        $file = $request->file('logFile');


        if($file->isValid()) {

            $clientName = $file -> getClientOriginalName();    //客户端文件名称..
            $tmpName = $file ->getFileName();   //缓存在tmp文件夹中的文件名例如php8933.tmp 这种类型的.
            $realPath = $file -> getRealPath();     //这个表示的是缓存在tmp文件夹下的文件的绝对路径
            $entension = $file -> getClientOriginalExtension();   //上传文件的后缀.
            $mimeTye = $file -> getMimeType();    //也就是该资源的媒体类型
            $date=date("Y-m-d");
            $newName = $newName = $date."_".date('H:i:s')."_".md5(date('ymdhis').$clientName).".".$entension;    //定义上传文件的新名称
            $dir=config('secret.file_log').$date;
            $path = $file -> move($dir,$newName);    //把缓存文件移动到制定文件夹

        }

        if($path){
            return json_encode(['code' => Response::HTTP_OK, 'msg' => '成功']);
        }else{
            return json_encode(['code' => Response::HTTP_FAIL, 'msg' => '上传失败']);
        }

    }



    public function ce(Request $request){
echo 123;
//        $data=$request->post();
//        $amqp=config('amqp.log');
//
//        $pro= new Log($amqp,'loglist');
//
//        $pro->production(json_encode($data));


//        $t1=time(); //获取程序1，开始的时间
//
//
//        for($i=0;$i<1000;$i++){
//            $time=time();
//
//            $data['log_id']='94e2ca7e10917c244b4ac93c5acf864'.$i;
//            $data['event_name']='log_error';
//            $user_id="1231".$i;
//            $data['datas']=[
//                "mac"=>'1C1B0DCCB4EE',
//                "net_type"=>2,
//                "device"=>"wait for java",
//                "err_msg"=>"is nil\nstack traceback:\n\tcommon\/log_utility.lua: in function 'printError'\n\tgame\/scene\/component\/bg_ctrl.lua: in function ''\n\tframework\/utilities\/asset_utility.lua: in function <framework\/utilities\/asset_utility.lua:0>\n\t[C]: in function 'resume'\n\tframework\/updater\/coroutine.lua: in function ''\n\tframework\/updater\/coroutine.lua: in function <framework\/updater\/coroutine.lua:0>\n\t[C]: in function 'pcall'\n\tframework\/updater\/timer.lua: in function 'Update'\n\tframework\/updater\/timer_manager.lua: in function <framework\/updater\/timer_manager.lua:0>\n\t[C]: in function ''\n\tcommon\/tools\/event.lua: in function <common\/tools\/event.lua:0>\n\tcommon\/tools\/event.lua: in function 'CoUpdateBeat'\n\tcommon\/tools\/event.lua: in function <common\/tools\/event.lua:0>\nstack traceback:\n\t[C]: in function 'error'\n\tcommon\/log_utility.lua: in function 'printError'\n\tgame\/scene\/component\/bg_ctrl.lua: in function ''\n\tframework\/utilities\/asset_utility.lua: in function <framework\/utilities\/asset_utility.lua:0>\n\t[C]: in function 'resume'\n\tframework\/updater\/coroutine.lua: in function ''\n\tframework\/updater\/coroutine.lua: in function <framework\/updater\/coroutine.lua:0>\n\t[C]: in function 'pcall'\n\tframework\/updater\/timer.lua: in function 'Update'\n\tframework\/updater\/timer_manager.lua: in function <framework\/updater\/timer_manager.lua:0>\n\t[C]: in function ''\n\tcommon\/tools\/event.lua: in function <common\/tools\/event.lua:0>\n\tcommon\/tools\/event.lua: in function 'CoUpdateBeat'\n\tcommon\/tools\/event.lua: in function <common\/tools\/event.lua:0>\r\n at XLua.LuaEnv.ThrowExceptionFromError (System.Int32 oldTop) [0x00040] in E:\\client_dev\\Assets\\cs_runtime\\3rd\\Xlua\\LuaEnv.cs:441 \r\n at XLua.DelegateBridge.Action[T1] (T1 p1) [0x0005e] in E:\\client_dev\\Assets\\cs_runtime\\3rd\\Xlua\\GenericDelegateBridge.cs:43 \r\n at MyProject.AssetReqMgr+<>c__DisplayClass38_0.<LoadAssetAsync>b__0 (MyProject.AssetReq r) [0x0015c] in <c4de0c4a61d84492a935267164c3a1db>:0 \r\n at MyProject.AssetReq.FireAllLoaded () [0x002a8] in <c4de0c4a61d84492a935267164c3a1db>:0 \r\n at MyProject.AssetReq.CheckAllTaskDone () [0x002e4] in <c4de0c4a61d84492a935267164c3a1db>:0 0x00007FF6AA10CF2C (Unity) StackWalker::GetCurrentCallstack\n0x00007FF6AA110EE1 (Unity) StackWalker::ShowCallstack\n0x00007FF6A8816025 (Unity) GetStacktrace\n0x00007FF6AAD8057E (Unity) DebugStringToFile\n0x00007FF6AA16B5D5 (Unity) DebugLogHandler_CUSTOM_Internal_Log\n0x0000023C072C97FB (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)\n0x0000023C072C967B (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])\n0x0000023C072C8EBC (Mono JIT Code) UnityEngine.Logger:LogFormat (UnityEngine.LogType,string,object[])\n0x0000023C086AB0EB (Mono JIT Code) UnityEngine.Debug:LogErrorFormat (string,object[])\n0x0000023C072C5813 (Mono JIT Code) CltLog:LevelLogFormat (CltLog\/LogLevel,string,object[])\n0x0000023C0752F163 (Mono JIT Code) MyProject.AssetReq:CheckAllTaskDone ()\n0x0000023C0752D593 (Mono JIT Code) MyProject.AssetReq:SetAllTargetInputed ()\n0x0000023C0752A9F3 (Mono JIT Code) MyProject.AssetReqMgr:LoadAssetAsync (string,int,System.Action",
//                "user_id"=>$user_id
//            ];
//
//
//        $arr=curl_post("http://client.log.rheingames.com:8081/blog/public/index.php/api/report",json_encode($data));
//        echo $arr;exit;
//
//
//        }
//        $t2=time(); //获取程序1，结束的时间
//        $time1=$t2-$t1;
//        echo $time1;exit;
    }

    public function a(){
 echo 123;
//        $config = [
//            'host' => '106.52.17.37',
//            'port' => '9001',
//            'username' => 'test',
//            'password' => 'abc123'
//        ];
//        $db = new ClickHouseDB\Client($config);
//        $db->database('default');
//        $db->setTimeout(1.5);      // 1500 ms
//        $db->setTimeout(10);       // 10 seconds
//        $db->setConnectTimeOut(5); // 5 seconds
//        print_r($db);
    }
}